package top.whysu.edevice.service.manage.impl;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import top.whysu.edevice.constant.CodeConstant;
import top.whysu.edevice.dao.manage.ManageDeviceDao;
import top.whysu.edevice.dao.relative.RelativeVoucherAccountDao;
import top.whysu.edevice.dto.ezviz.token.TokenWithAccountDTO;
import top.whysu.edevice.dto.monitor.MonitorAddressResponseDTO;
import top.whysu.edevice.exception.BusinessException;
import top.whysu.edevice.po.manage.ManageDevicePO;
import top.whysu.edevice.po.system.AccountPO;
import top.whysu.edevice.service.ezviz.EzvizHttpService;
import top.whysu.edevice.service.ezviz.EzvizTokenService;
import top.whysu.edevice.service.manage.MonitorService;
import top.whysu.edevice.service.system.AccountService;
import top.whysu.edevice.service.system.RoleService;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 萤石视频监控
 */
@Service("MonitorServiceImpl")
public class MonitorServiceImpl implements MonitorService {
    private static final Logger LOGGER = LoggerFactory.getLogger(MonitorServiceImpl.class);

    @Resource
    private AccountService accountService;
    @Resource
    private EzvizTokenService ezvizTokenService;
    @Resource
    private EzvizHttpService ezvizHttpService;
    @Resource
    private ManageDeviceDao manageDeviceDao;
    @Resource
    private RoleService roleService;
    @Resource
    private RelativeVoucherAccountDao relativeVoucherAccountDao;

    @Override
    public MonitorAddressResponseDTO getLiveAddress(BigDecimal deviceId) {
        if (deviceId == null) {
            throw new BusinessException(CodeConstant.Http.INFO_ERROR, "设备id不能为空！");
        }
        // 获取当前登录用户
        String currentUserName;
        try {
            currentUserName = (String) SecurityUtils.getSubject().getPrincipal();
        } catch (Exception e) {
            LOGGER.error("获取当前用户异常", e);
            throw new BusinessException(CodeConstant.Http.EXECUTE_FAIL, "获取当前登录用户信息失败！请重新登录再尝试！");
        }
        // 查询设备是否存在
        ManageDevicePO manageDevicePO = manageDeviceDao.queryById(deviceId);
        if (manageDevicePO == null) {
            throw new BusinessException(CodeConstant.Http.INFO_ERROR, "设备不存在，请刷新页面后重试！");
        }
        // 查询账号信息
        AccountPO accountPO = accountService.selectByUsername(currentUserName);
        if (accountPO == null || accountPO.getId() == null) {
            throw new BusinessException(CodeConstant.Http.EXECUTE_FAIL, "获取用户信息失败！");
        }
        // 获取子账号的accessToken
        TokenWithAccountDTO tokenWithAccountDTO = ezvizTokenService.getChildAccountToken(accountPO.getId());
        // 封装地址。预览地址（高清）： ezopen://open.ys7.com/F09067012/1.hd.live
        // 预览地址(流畅)：ezopen://open.ys7.com/F09067012/1.live
        // 该设备如果已被加密，请或在ezopen地址中加入设备验证码，加入示例如下：ezopen://验证码@open.ys7.com/203751922/1.rec
        MonitorAddressResponseDTO addressResponseDTO = new MonitorAddressResponseDTO();
        // 这里返回的是子账号的accessToken
        addressResponseDTO.setAccessToken(tokenWithAccountDTO.getAccessToken());
        addressResponseDTO.setUrl("ezopen://" + manageDevicePO.getDeviceCode() + "@open.ys7.com/" + manageDevicePO.getDeviceSerial() + "/1.hd.live");
        return addressResponseDTO;
    }
}